<?php

namespace App\Http\Controllers;

use App\Comment;
use \App\Post;
use \App\Zan;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        //利用日志容器去写日志
//        $app = app();
//        $log = $app->make("log");
        //我们换一个依赖注入的方法 public function index(\Psr\Log\LoggerInterface $log)
        //$log->info("post_index",['data'=>'this is post index']);
//      门脸写法
//        \Log::info("post_index",['data'=>'this is post index']);

        $posts = Post::orderBy('created_at', 'desc')->withCount(['comments','zans'])->paginate(6);
        return view('post/index', compact('posts'));
    }

    public function show(Post $post)
    {
        //模型预加载
        $post->load('comments');
        return view('post/show', compact('post'));
    }

    public function create()
    {
        return view('post/create');
    }

    //创建文章
    public function store()
    {
        //验证
        $this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10',
        ], [
            'title.min' => '文章标题过短',    //自定义中文说明
        ]);
        //逻辑
        //把用户id一起存进表
        $user_id = \Auth::id();
        $params = array_merge(request(['title','content']),compact('user_id'));
        Post::create($params);
        //渲染
        return redirect('/posts');
    }

    public function edit(Post $post)
    {
        return view('post/edit', compact('post'));
    }


    public function update(Post $post)
    {
        //验证
        $this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10',
        ], [
            'title.min' => '文章标题过短',
        ]);
        //用策略判断权限
        $this->authorize('update',$post);

        //逻辑
        $post->title = request('title');
        $post->content = request('content');
        $post->save();

        //渲染
        return redirect("/posts/{$post->id}");

    }

    public function delete(Post $post)
    {
        $this->authorize('delete',$post);
        $post->delete();
        return redirect("/posts");
    }

    //上传文件
    public function imageUpload(Request $request)
    {
        $path = $request->file('wangEditorH5File')->storePublicly(md5(time()));
        return asset('storage/' . $path);
    }

    //提交评论
    public function comment(Post $post) {
        //验证
        $this->validate(request(), [
            'content' => 'required|min:3',
        ]);
        //逻辑
        $comments = new Comment();
        $comments->user_id = \Auth::id();
        $comments->content = request('content');
        $post->comments()->save($comments);

        //渲染
        return back();
    }

    //赞
    public function zan(Post $post){

        $param = [
            'user_id' => \Auth::id(),
            'post_id' => $post->id,
        ];
        //模型里去查找，有就读出来，没就创建
        Zan::firstOrCreate($param);
        return back();
    }

    //取消赞
    public function unzan(Post $post){

        $post->zan(\Auth::id())->delete();
        return back();
    }

    //搜索结果页
    public function search(){

        //验证
        $this->validate(request(), [
            'query' => 'required',
        ]);
        //逻辑
        $query = request('query');
        $posts = \App\Post::search($query)->paginate(6);
        //渲染
        return view("post/search",compact('posts','query'));
    }
}
